/*
 *      game.cpp
 *      
 *      2009 Víctor Manuel Ochoa <kanito8a@gmail.com>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

#include "game.h"

/*
 * 
 * name: desconocido
 * @param
 * @return
 */
Game::Game()
{	
}

/*
 * 
 * name: desconocido
 * @param
 * @return
 */
Game::~Game()
{
}

/*
 * 
 * name: desconocido
 * @param
 * @return
 */
bool Game::init()
{
	
	m_pLog=&Log::getInstance();
	m_pLog->setColors(true);

	if(!m_video.init(PROGRAM_NAME) || !m_game_manager.init() || !m_game_textures.init())
		return false;
		
	/****************************************************/
	/* PRUEBAS PRUEBAS PRUEBAS  PRUEBAS PRUEBAS PRUEBAS */
	/****************************************************/
	int texnum=10;	
	
	
	Texture *tex[texnum];
	for(int i=1;i<texnum;i++)tex[i]=0;	
	
	tex[0]=m_game_textures.newTexture("data/pixmaps/noxii_basic.png");
	tex[1]=m_game_textures.newTexture("data/torch_flame_01_1.png");
	tex[2]=m_game_textures.newTexture("data/exp.png");
	tex[3]=m_game_textures.newTexture("data/cabeza.png");
	tex[4]=m_game_textures.newTexture("data/pixmaps/sword.png");
	tex[5]=m_game_textures.newTexture("data/camuf.png");
	tex[6]=m_game_textures.newTexture("data/cargando.png");

	//~ FileReader fr;
	//~ fr.init("data/cfg/noxii.cfg");
	//~ //printf("Hemos encontrado: %s\n",fr.find("rows"));
	//~ printf("Hemos encontrado: %s\n",fr.readField(fr.find("filename"),1));
	//~ AnimedEntity *prueba;
	//~ prueba=new AnimedEntity();
	//~ prueba->init(&m_game_textures,"data/cfg/noxii_body.cfg");
	
	//~ Noxii *pnoxii;
	//~ pnoxii=new Noxii();
	//~ pnoxii->init(&m_game_manager,&m_game_textures);
	//~ pnoxii->setX(WIDTH/2);
	//~ pnoxii->setY(HEIGHT/2);
	
	Level *level=new Level();
	level->init(&m_game_textures,"data/cfg/level.cfg");
	level->setZ(0.f);
	
	//~ fr.freeMemory();
	//~ Point *point;
	//~ point=new Point();
	//~ point->init();
	//~ point->setX(110);
	
	//~ Noxii *noxii;
	//~ noxii=new Noxii();
	//~ noxii->init(&m_game_textures);
	//~ noxii->setGroupManager(m_game_manager.getGroupManager());

	//~ Particles *part=new Particles();
	//~ part->init(tex[1],500,PART_FIRE,1.5f,0.5f,160.f,129.f,0.f,1.f,0.15f,0.15f);
	//~ part->setAngle(-10);
	//~ part->setSpeed(0.2f);
	//~ part->setAcceleration(0.5f);
	
	Skydome *sky;
	sky=new Skydome();
	
	float c1[3]={0.1f,0.1f,0.9f};
	float c2[3]={1.0f,1.0f,1.0f};
	
	sky->init(c1,c2);
	sky->setZ(99.f);
	
	//~ noxii->setSpeed(0.4f);
	//~ noxii->setX(210);
	//~ noxii->setZ(-10);
	//~ m_game_manager.addEntity(part);
	//~ m_game_manager.addEntity(noxii);
	//~ m_game_manager.addEntity(point);	
	//~ prueba->setX(320);
	//~ prueba->setY(240);
	
	m_game_manager.addEntity(sky);
	//~ m_game_manager.addEntity(pnoxii);
	m_game_manager.addEntity(level);
	
	//~ NOXII_DE_PRUEBA=noxii;
	
	for(int i=1;i<texnum;i++)tex[i]=0;
	
	/****************************************************/
	/* PRUEBAS PRUEBAS PRUEBAS  PRUEBAS PRUEBAS PRUEBAS */
	/****************************************************/
	
	m_end_game=false;	
	m_pLog->debug("+ Game");
	return true;
}

/*
 * 
 * name: desconocido
 * @param
 * @return
 */
void Game::play()
{
  	while(!m_end_game)
	{ 	  
		m_timer.reset();
    	
		keyUpdate();
		
		unsigned int frametime;
		do 
		{
			frametime=m_timer.currentTime();
		} while (frametime<20);			

		m_game_manager.draw();		
		
		m_video.update();
		m_game_manager.update();
  	}	
}

/*
 * 
 * name: desconocido
 * @param
 * @return
 */
void Game::freeMemory()
{	
	m_game_manager.freeMemory();
	m_game_textures.freeMemory();
	m_video.freeMemory();
	m_pLog->debug("- Game");
	m_pLog->freeMemory();
	m_pLog=0;
}

/*
 * 
 * name: desconocido
 * @param
 * @return
 */
void Game::keyUpdate()
{
	SDL_Event event;
	
	while ( SDL_PollEvent( &event ) )
	{
		switch( event.type )
		{
			case SDL_ACTIVEEVENT:
				if ( event.active.gain == 0 )
				{
					m_active = false;
				}
				else
				{
					m_active = true;
				}
			break;

			case SDL_QUIT:
				m_end_game = true;
			break;
		  
			case SDL_KEYDOWN:
				if(event.key.keysym.sym==SDLK_ESCAPE || event.key.keysym.sym==SDLK_RETURN)
					m_end_game = true;					
				
				if(event.key.keysym.sym==SDLK_d){
					//~ if(NOXII_DE_PRUEBA->isFlipped())NOXII_DE_PRUEBA->flip();
					//~ NOXII_DE_PRUEBA->setAngle(0);
					//~ NOXII_DE_PRUEBA->setSpeed(1);
					//~ NOXII_DE_PRUEBA->setState(STATE_RUNNING);
				}

				if(event.key.keysym.sym==SDLK_a){
					//~ if(!NOXII_DE_PRUEBA->isFlipped())NOXII_DE_PRUEBA->flip();
					//~ NOXII_DE_PRUEBA->setAngle(180);
					//~ NOXII_DE_PRUEBA->setSpeed(1);
					//~ NOXII_DE_PRUEBA->setState(STATE_RUNNING);
				}
			break;		

			case SDL_KEYUP:
				if(event.key.keysym.sym==SDLK_ESCAPE || event.key.keysym.sym==SDLK_RETURN)
					m_end_game = true;					
				
				if(event.key.keysym.sym==SDLK_d){
					
					//~ NOXII_DE_PRUEBA->setAngle(0);
					//~ NOXII_DE_PRUEBA->setSpeed(0);	
					//~ NOXII_DE_PRUEBA->setState(STATE_STAND);				
				}

				if(event.key.keysym.sym==SDLK_a){
					
					//~ NOXII_DE_PRUEBA->setAngle(180);
					//~ NOXII_DE_PRUEBA->setSpeed(0);
					//~ NOXII_DE_PRUEBA->setState(STATE_STAND);				
				}
			break;		

			default:
			break;
		}
  	}
}
